import { get, add } from "../../utils/db"
import { upload, multiUpload } from "../../utils/tools";
const app = getApp()

Page({
  data: {
    //菜单名
    list: [],//菜单分类列表
    files: [],
    newArr: []//临时图片数组
  },
  async onLoad() {
    //读取菜谱分类
    var result = await get("class");
    this.setData({
      list: result.data
    })
  },
  selectImage(e) {
    // console.log(e);
    //图片地址传到data中
    this.data.newArr = e.detail.tempFilePaths
    var arr = e.detail.tempFilePaths
    var files = arr.map(item => {
      return { url: item }
    })
    // console.log(files);
    this.setData({
      files
    })
  },
  async mySubmit(e) {
    wx.showLoading({
      title: '正在提交',
      mask: true
    })

    // console.log(e);
    //图片上传
    var arr = this.data.newArr; //获取到临时图片地址
    var result = await multiUpload(arr)
    var image = result.map(item => {
      return item.fileID
    })
    // console.log(image);
    //上传到数据库
    // var data = { realname: this.data.realname, image: image }
    var data = e.detail.value;
    data.image = image;
    data.avatarUrl = app.globalData.userInfo.avatarUrl;
    data.nickName = app.globalData.userInfo.nickName;
    data.views = 0;
    data.likes = 0;
    data.addtime = new Date().getTime()
    data.status = 1;
    var result = await add("menu", data)
    //交互效果
    wx.hideLoading()
    wx.showToast({
      title: '添加成功',
    })
    setTimeout(() => {
      wx.navigateBack()
    }, 1500);
  }
})